package com.moon.game.util;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import org.apache.commons.lang3.time.DateUtils;
import org.apache.commons.lang3.time.DurationFormatUtils;

/**
 * @author bo
 * @create date:2010-1-2
 * 
 */
public class GDateUtils {

	public static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";

	public static final String DATE_FORMAT = "yyyy-MM-dd";

	public static final String TIME_FORMAT = "HH:mm:ss";
	
	
	public static final long MILLIS_PER_SECOND = 1000;
	public static final long MILLIS_PER_MINUTE = 60 * MILLIS_PER_SECOND;
	public static final long MILLIS_PER_HOUR = 60 * MILLIS_PER_MINUTE;
	public static final long MILLIS_PER_DAY = 24 * MILLIS_PER_HOUR;
	
	
	/**
     * 两个时间是否为同一天
     * 
     * @param time1
     *            秒
     * @param time2
     *            秒
     * @return
     */
    public static boolean isSameDay(int time1, int time2) {
        return DateUtils.isSameDay(new Date(time1 * MILLIS_PER_SECOND), new Date(time2 * MILLIS_PER_SECOND));
    }

    /**
     * 两个时间是否为同一天
     * 
     * @param time1
     * @param time2
     * @return
     */
    public static boolean isSameDay(Date time1, Date time2) {
        return DateUtils.isSameDay(time1, time2);
    }

    /**
     * 两个时间是否为同一天
     * 
     * @param time1
     * @param time2
     * @return
     */
    public static boolean isSameDay(Calendar time1, Calendar time2) {
        return DateUtils.isSameDay(time1, time2);
    }

    /**
     * 倒计时
     * 
     * @param duration
     * @param returnMsg
     * @return
     */
    public static String countdown(final long duration, final String returnMsg) {
        return countdown(duration, true, false, "", returnMsg);
    }

    /**
     * 倒计时
     * 
     * @param duration
     *            时间差秒钟
     * @param isDisplaySecond
     *            是否显示秒
     * @param isDisplayPrefix
     *            当结束时是否显示前缀
     * @param prefix
     *            前缀消息,可以根据需要设置参数
     * @param returnMsg
     *            结束时的返回消息,可以根据需要设置返回参数
     * @return
     */
    public static String countdown(final long duration, final boolean isDisplaySecond, final boolean isDisplayPrefix, final String prefix, final String returnMsg) {
        if (duration <= 0) {
            return isDisplayPrefix ? prefix + returnMsg : returnMsg;
        }

        long tempMinute = isDisplaySecond ? duration : (duration / 60 * 60);
        long minuteDuration = (tempMinute == 0) ? 60 :tempMinute; 
        String ret = DurationFormatUtils.formatDurationWords(minuteDuration * MILLIS_PER_SECOND, true, true);
        return prefix + ret.replaceAll("[\\s]+", "").replaceAll("day[s]?", "天").replaceAll("hour[s]?", "小时").replaceAll("minute[s]?", "分钟").replaceAll("second[s]?", "秒");
    }

    /**
     * 倒计时
     * 
     * @param duration
     *            时间差秒钟
     * @param isDisplaySecond
     *            是否显示秒
     * @param isDisplayPrefix
     *            当结束时是否显示前缀
     * @param prefix
     *            前缀消息,可以根据需要设置参数
     * @param returnMsg
     *            结束时的返回消息,可以根据需要设置返回参数
     * @return
     */
    public static String simpleCountdown(final long duration, final boolean isDisplaySecond, final boolean isDisplayPrefix, final String prefix, final String returnMsg) {
        if (duration <= 0) {
            return isDisplayPrefix ? prefix + returnMsg : returnMsg;
        }

        long tempMinute = isDisplaySecond ? duration : (duration / 60 * 60);
        long minuteDuration = (tempMinute == 0) ? 60 :tempMinute; 
        String ret = DurationFormatUtils.formatDurationWords(minuteDuration * MILLIS_PER_SECOND, true, true);
        return prefix + ret.replaceAll("[\\s]+", "").replaceAll("day[s]?", "天").replaceAll("hour[s]?", "时").replaceAll("minute[s]?", "分").replaceAll("second[s]?", "秒");
    }
    
    /*public static void main(String[] args) {
    	System.out.println(simpleCountdown(0 , true,false, "","已经结束"));
	}*/

    /**
     * 计算两个时间之间相隔的天数
     * 
     * @param startTime
     *            开始时间,以秒为单位
     * @param endTime
     *            结束时间,以秒为单位
     * @return
     */
    public static int dayDelta(int startTime, int endTime) {
        if (startTime > endTime) {
            return 0;
        }

        GregorianCalendar cal1 = new GregorianCalendar();
        GregorianCalendar cal2 = new GregorianCalendar();

        cal1.setTime(new Date(startTime * 1000l));
        cal2.setTime(new Date(endTime * 1000l));

        int year1 = cal1.get(Calendar.YEAR);
        int year2 = cal2.get(Calendar.YEAR);

        int delta = 0;
        if (year1 < year2) {
            if (cal1.isLeapYear(year1)) {
                delta = 366 - cal1.get(Calendar.DAY_OF_YEAR);
            } else {
                delta = 365 - cal1.get(Calendar.DAY_OF_YEAR);
            }

            delta += cal2.get(Calendar.DAY_OF_YEAR);
        } else {
            delta = cal2.get(Calendar.DAY_OF_YEAR) - cal1.get(Calendar.DAY_OF_YEAR);
        }

        return delta;
    }

    public static String formatDate(Date date, String format) {
        return new SimpleDateFormat(format).format(date);
    }

    public static String formatDate(String format) {
        return new SimpleDateFormat(format).format(new Date());
    }

    public static String formatTime(int time, String pattern) {
        try {
            return new SimpleDateFormat(pattern).format(new Date(time * MILLIS_PER_SECOND));
        } catch (Exception e) {
            return "";
        }
    }

    public static String formatTime(int time) {
        return formatTime(time, "HH:mm");
    }

    /**
     * 当前时间秒
     * 
     * @return 当前时间秒
     */
    public static int now() {
        return (int) (System.currentTimeMillis() / MILLIS_PER_SECOND);
    }
    
    /**
	 * 获取当前时间，格式返回毫秒 
	 * @return
	 */
	public static long getMillisNow() {
		return System.currentTimeMillis();
	}

    /**
     * TimeAgo
     * 
     * @param reference
     *            参考时间
     * @param then
     *            此时
     * @param format
     *            格式化
     * @param daysFormat
     *            超过此天数,将按format格式化显示实际时间
     * @return
     */
    private static String timeAgo(final Date reference, Date then, final String format, final int daysFormat) {
        final long distanceMillis = Math.abs(then.getTime() - reference.getTime());
        final int sencond = (int) (distanceMillis / MILLIS_PER_SECOND);
        final int minutes = (int) (distanceMillis / MILLIS_PER_MINUTE);
        final int hours = (int) (distanceMillis / MILLIS_PER_HOUR);
        final int days = (int) (distanceMillis / MILLIS_PER_DAY);

        String message = "刚刚";
        if (days > 0) {
            message = (days >= daysFormat) ? new SimpleDateFormat(format).format(reference) : days + "天前";
        } else if (hours > 0) {
            message = hours + "小时前";
        } else if (minutes > 0) {
            message = minutes + "分钟前";
        } else if (sencond >= 30) {
            message = sencond + "秒钟前";
        }
        return message;
    }

    /**
     * 得到明天零点时间
     * 
     * @return
     */
    public static long getTimeTommorowDiff() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_MONTH, 1);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTimeInMillis();
    }
    
    public static String formatSeconds(int second) {
    	String time = "";
		if (second > 3600) {
			time += (second / 3600) + "小时" + (second % 3600 / 60) + "分";
		}else if (second > 60 && second <= 3600) {
			time += (second / 60) + "分" + (second % 60) + "秒";
		}else {
			time += second + "秒";
		}
		return time;
    }

    /**
     * 将long型时间转换成format形式日期
     * 
     * @param date
     *            如:1260325758234
     * @return
     */
    public static String formatFromLong(long date, String format) {

        SimpleDateFormat df = new SimpleDateFormat(format);

        String res = df.format(date);

        return res;

    }
    public static void main(String[] args) {
    	System.out.println(System.currentTimeMillis());
    	String s = formatFromLong(System.currentTimeMillis(),"yyyy年MM月dd日 HH时mm分ss");
    	System.out.println(s);
	}

	private GDateUtils() {
	}

	public static Timestamp getSysTime() {

		Timestamp sys_time = new Timestamp(Calendar.getInstance()
				.getTimeInMillis());

		return sys_time;
	}
	
	/** 
	 * 获取当前时间，格式2010-08-03 14:10:04
	 * @return
	 */
	public static String getStringDate() {
		   Date currentTime = new Date();
		   SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		   String dateString = formatter.format(currentTime);
		   return dateString;
	}
	
	/**
	 * Convert string to Date
	 * 
	 * @return a java.util.Date object converted.
	 */
	public static java.util.Date stringToDate(String pstrValue,
			String pstrDateFormat) {
		if ((pstrValue == null) || (pstrValue.equals(""))) {
			return null;
		}
		java.util.Date dttDate = null;
		try {
			SimpleDateFormat oFormatter = new SimpleDateFormat(pstrDateFormat);
			dttDate = oFormatter.parse(pstrValue);
			oFormatter = null;
		} catch (Exception e) {
			e.printStackTrace();
		}

		return dttDate;
	}

	/**
	 * Date convert to String.
	 * 
	 * @return String representation of the given Date and DateFormat.
	 */
	public static String dateToString(Date pdttValue, String pstrDateFormat) {
		String pstrDate = null; // return value
		if (pdttValue == null) {
			return null;
		}
		if ((pstrDateFormat == null) || (pstrDateFormat.equals(""))) {
			pstrDateFormat = "yyyy-MM-dd";
		}
		SimpleDateFormat oFormatter = new SimpleDateFormat(pstrDateFormat);
		pstrDate = oFormatter.format(pdttValue);
		return pstrDate;
	}
	
	/***
	 * 生成现在的时间格式字体串
	 * @param pstrDateFormat
	 * @return
	 */
	public static String getCurDateToString(String pstrDateFormat){
		String pstrDate = null; // return value
		Date curDate = new Date();
		if ((pstrDateFormat == null) || (pstrDateFormat.equals(""))) {
			pstrDateFormat = "yyyy-MM-dd";
		}
		SimpleDateFormat oFormatter = new SimpleDateFormat(pstrDateFormat);
		pstrDate = oFormatter.format(curDate);
		return pstrDate;
		
	}

	/**
	 * DateTime convert to String.
	 * 
	 * @return String representation of the given DateTime and DateFormat.
	 */
	public static String dateTimeToString(java.sql.Timestamp dt, String df) {
		String pstrDate = null; // return value
		if (dt == null)
			return "";
		if ((df == null) || (df.equals(""))) {
			df = "yyyy-MM-dd HH:mm:ss";
		}
		SimpleDateFormat oFormatter = null;
		try {
			oFormatter = new SimpleDateFormat(df);
			pstrDate = oFormatter.format(dt);
		} catch (Exception e) {
			oFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			pstrDate = oFormatter.format(dt);
		}
		return pstrDate;
	}

	/**
	 * String convert to SQLDate.
	 * 
	 * @return a java.sql.Date representatio of the given date string and format
	 *         string.
	 */
	public static java.sql.Date stringToSQLDate(String pstrValue,
			String pstrDateFormat) throws ParseException {
		if ((pstrValue == null) || (pstrValue.equals(""))) {
			return null;
		}
		java.util.Date dttTempDate = stringToDate(pstrValue, pstrDateFormat);
		return new java.sql.Date(dttTempDate.getTime());
	}

	/**
	 * String convert to SQLTime.
	 * 
	 * @return a java.sql.Time representation of the given date string and
	 *         format string.
	 */
	public static java.sql.Time stringToSQLTime(String pstrValue,
			String pstrDateFormat) throws ParseException {
		if ((pstrValue == null) || (pstrValue.equals(""))) {
			return null;
		}
		java.util.Date dttTempDate = stringToDate(pstrValue, pstrDateFormat);
		return new java.sql.Time(dttTempDate.getTime());
	}

	/**
	 * String convert to SQLTimestamp.
	 * 
	 * @return a java.sql.Timestamp representation of the given date string and
	 *         format string.
	 */
	public static java.sql.Timestamp stringToSQLTimestamp(String pstrValue,
			String pstrDateFormat) throws ParseException {
		if ((pstrValue == null) || (pstrValue.equals(""))) {
			return null;
		}
		java.util.Date dttTempDate = stringToDate(pstrValue, pstrDateFormat);
		return new java.sql.Timestamp(dttTempDate.getTime());
	}

	/**
	 * Get current date in form of java.sql.Date.
	 * 
	 * @return java.sql.Date object of current date.
	 */
	public static java.sql.Date getCurSQLDate() {
		return new java.sql.Date(new Date().getTime());
	}

	/**
	 * Get current date in form of java.sql.Timestamp.
	 * 
	 * @return java.sql.Timestamp object of current date.
	 */
	public static java.sql.Timestamp getCurSQLDateTime() {
		return new java.sql.Timestamp(new Date().getTime());
	}

	/**
	 * Get current date in form of java.sql.Date and then convert it into
	 * String. The return date string follow that of the input param DateFormat.
	 * 
	 * @return String representation of current date.
	 */
	public static String getCurSQLDateInString(String pstrDateFormat) {
		String pstrDateTime = null;
		if ((pstrDateFormat != null) && (!pstrDateFormat.equals(""))) {
			java.util.Date curDateTime = new java.util.Date();
			java.sql.Date curSqlDateTime = new java.sql.Date(curDateTime
					.getTime());
			SimpleDateFormat sdf = new SimpleDateFormat(pstrDateFormat);
			pstrDateTime = sdf.format(curSqlDateTime);
		}
		return pstrDateTime;
	}

	/**
	 * Get current date in form of java.sql.Timestamp and then convert it into
	 * String. The return date string follow that of the input param DateFormat.
	 * 
	 * @return String representation of current date.
	 */
	public static String getCurSQLDateTimeInString(String pstrDateFormat) {
		String pstrDateTime = null;
		if ((pstrDateFormat != null) && (!pstrDateFormat.equals(""))) {
			java.util.Date curDateTime = new java.util.Date();
			java.sql.Timestamp curSqlDateTime = new java.sql.Timestamp(
					curDateTime.getTime());
			SimpleDateFormat sdf = new SimpleDateFormat(pstrDateFormat);
			pstrDateTime = sdf.format(curSqlDateTime);
		}
		return pstrDateTime;
	}

	/**
	 * check the date by pattern
	 * 
	 * @param sDateValue
	 * @param sDateFormat
	 * @return boolean,if check the date ok,then return true
	 * @throws ParseException
	 */
	public static boolean checkDateByMask(String sDateValue, String sDateFormat)
			throws ParseException {
		boolean isTrue = false;
		if (sDateValue == null || sDateFormat == null || sDateValue.equals(""))
			return false;
		if (sDateValue.length() != sDateFormat.length())
			return false;
		java.util.Date date = stringToDate(sDateValue, sDateFormat);
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		if (sDateFormat.indexOf("yyyy") > -1) {
			isTrue = (cal.get(Calendar.YEAR) == Integer.parseInt(sDateValue
					.substring(sDateFormat.indexOf("yyyy"), sDateFormat
							.indexOf("yyyy") + 4))
					&& cal.get(Calendar.MONTH) == (Integer.parseInt(sDateValue
							.substring(sDateFormat.indexOf("MM"), sDateFormat
									.indexOf("MM") + 2)) - 1) && cal
					.get(Calendar.DATE) == Integer.parseInt(sDateValue
					.substring(sDateFormat.indexOf("dd"), sDateFormat
							.indexOf("dd") + 2)));
		} else {
			isTrue = (cal.get(Calendar.YEAR) == Integer.parseInt(sDateValue
					.substring(sDateFormat.indexOf("yy"), sDateFormat
							.indexOf("yy") + 2))
					&& cal.get(Calendar.MONTH) == (Integer.parseInt(sDateValue
							.substring(sDateFormat.indexOf("MM"), sDateFormat
									.indexOf("MM") + 2)) - 1) && cal
					.get(Calendar.DATE) == Integer.parseInt(sDateValue
					.substring(sDateFormat.indexOf("dd"), sDateFormat
							.indexOf("dd") + 2)));
		}
		return isTrue;
	}

	/**
	 * get current date by local pattern
	 * 
	 * @param ifdatetime
	 * @return String
	 */
	public static String getNowWithLocal(boolean ifdatetime) {
		java.util.Date dd = new java.util.Date();
		if (ifdatetime) {
			return java.text.DateFormat.getDateTimeInstance(
					java.text.DateFormat.LONG, java.text.DateFormat.LONG)
					.format(dd);
		} else {
			return java.text.DateFormat.getDateInstance(
					java.text.DateFormat.LONG).format(dd);
		}
	}

	// 取得当前系统时间with yyyy-mm-dd 格式
	public static String getNow(boolean ifdatetime) {
		java.util.Date dd = new java.util.Date();
		java.text.SimpleDateFormat df = null;
		String rtn = "";
		if (ifdatetime) {
			df = new java.text.SimpleDateFormat(DATE_TIME_FORMAT);
		} else {
			df = new java.text.SimpleDateFormat(DATE_FORMAT);
		}
		rtn = df.format(dd);
		return rtn;
	}
	
	public static String getNowFormatLog(){
		String LOG_DATE_FORMAT="yyyyMMdd HH:mm:ss";
		java.util.Date dd = new java.util.Date();
		java.text.SimpleDateFormat df = null;
		String rtn = "";
		df = new java.text.SimpleDateFormat(LOG_DATE_FORMAT);
		rtn = df.format(dd);
		return rtn;
	}

	/**
	 * 用于从datetime中取得date
	 * 
	 * @param strDate
	 *            String 从数据库中取得的日期型数据
	 * @return String 以yyyy-mm-dd返回日期
	 */
	public static String subDate(String strDate) {
		if (strDate.length() > 10) {
			int pos = strDate.indexOf(" ");
			if (pos > 0) {
				return strDate.substring(0, pos);
			} else {
				return strDate;
			}
		} else {
			return strDate;
		}
	}

	// 用于从datetime中取得time
	public static String subTime(String strDate) {
		if (strDate.length() >= 8) {
			int pos = strDate.indexOf(" ");
			int pos1 = strDate.indexOf(".");
			if (pos1 <= 0)
				pos1 = strDate.length();
			if (pos > 0) {
				return strDate.substring(pos + 1, pos1);
			} else {
				if (strDate.indexOf("-") > 0)
					return "";
				else
					return strDate;
			}
		} else {
			return strDate;
		}
	}

	// 用于从datetime中取得datetime
	public static String subDateTime(String strDate) {
		if (strDate.length() > 10) {
			int pos = strDate.indexOf(".");
			if (pos > 0) {
				return strDate.substring(0, pos);
			} else {
				return strDate;
			}
		} else {
			return strDate;
		}
	}

	/**
	 * 给定两个日期，返回日期的天数差
	 * 
	 * @param startDate
	 * @param endDate
	 * @return int
	 */
	public static int getDayNumber(Date startDate, Date endDate) {
		Calendar startCal = Calendar.getInstance();
		startCal.setTime(startDate);
		Calendar endCal = Calendar.getInstance();
		endCal.setTime(endDate);
		if (startCal.after(endCal)) {
			return 0;
		}
		int num = 1;
		while (startCal.before(endCal)) {
			startCal.add(Calendar.DATE, 1);
			num++;
		}
		return num;
	}

	public static String getDuration(Timestamp createTime, Timestamp finishTime) {
		String rtn = "0小时";
		if (createTime == null) {
			return rtn;
		}
		long ctime = createTime.getTime();
		long ftime = System.currentTimeMillis();
		if (finishTime != null) {
			ftime = finishTime.getTime();
		}
		int dur = (int) (((ftime - ctime) / 1000) / 60);// min
		double dur_day = (double) dur / (60 * 24);
		if (dur_day > 1) {
			// rtn="1天以上";
			DecimalFormat format = new DecimalFormat("###.##");
			rtn = format.format(dur_day) + "天";
		} else {
			double dur_hour = (double) dur / 60;
			DecimalFormat format = new DecimalFormat("###.##");
			rtn = format.format(dur_hour) + "小时";
		}

		return rtn;

	}

	public static String getDurationMinute(Timestamp createTime,
			Timestamp finishTime) {
		String rtn = "0min";
		if (createTime == null) {
			return rtn;
		}
		long ctime = createTime.getTime();
		BigDecimal ct = new BigDecimal(Long.toString(ctime));
		long ftime = System.currentTimeMillis();
		if (finishTime != null) {
			ftime = finishTime.getTime();
		}
		BigDecimal ft = new BigDecimal(Long.toString(ftime));
		BigDecimal a = ft.subtract(ct);
		BigDecimal o = new BigDecimal("0");
		if (a.compareTo(o) != 0) {
			BigDecimal thonsand = new BigDecimal("1000");
			BigDecimal b = a.divide(thonsand, 2);
			BigDecimal sixty = new BigDecimal("60");
			double du = b.divide(sixty, 2, BigDecimal.ROUND_HALF_UP)
					.doubleValue();
			DecimalFormat format = new DecimalFormat("###.##");
			rtn = format.format(du) + "min";
		}
		return rtn;
	}

	public static String getDurationHour(Timestamp time1, Timestamp time2,
			String direction) {
		String f = direction;
		if (f == null) {
			f = "+";
		}
		long ctime1 = System.currentTimeMillis();
		long ctime2 = System.currentTimeMillis();
		if (time1 != null) {
			ctime1 = time1.getTime();
		}
		if (time2 != null) {
			ctime2 = time2.getTime();
		}
		int dur = 0;
		if ("+".equals(f)) {// time2-time1
			dur = (int) (((ctime2 - ctime1) / 1000) / 60);// min
		} else {
			dur = (int) (((ctime1 - ctime2) / 1000) / 60);// min
		}

		double dur_hour = (double) dur / 60;
		DecimalFormat format = new DecimalFormat("###.##");
		return format.format(dur_hour) + "hr";

	}

	/**
	 * 给定结束时间和天数,返回开始时间
	 * 
	 * @param Date
	 *            endDate
	 * @param int days
	 * @return String
	 */
	public static String getStartDate(int days, String strEndDate) {
		Calendar endCal = Calendar.getInstance();
		Date endDate = stringToDate(strEndDate, DATE_TIME_FORMAT);
		endCal.setTime(endDate);
		endCal.add(Calendar.DATE, -days);
		SimpleDateFormat sdf = new java.text.SimpleDateFormat(DATE_TIME_FORMAT);
		return sdf.format(endCal.getTime());

	}

	/**
	 * 获取当日开始时间
	 * 
	 * @return
	 */
	public static Date getDayStartDate(Date date) {
		GregorianCalendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		return calendar.getTime();
	}

	public static Timestamp getDayStartTimestamp(Date date) {
		Date start = getDayStartDate(date);
		return new Timestamp(start.getTime());
	}

	/**
	 * 获取下一日开始时间
	 * 
	 * @param date
	 * @return
	 */
	public static Date getNextDayStartDate(Date date) {
		GregorianCalendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);

		calendar.add(Calendar.DAY_OF_MONTH, 1);
		return calendar.getTime();
	}

	public static Timestamp getNextDayStartTimestamp(Date date) {
		Date start = getNextDayStartDate(date);
		return new Timestamp(start.getTime());
	}

	/**
	 * 获取月开始时间
	 * 
	 * @return
	 */
	public static Date getMonthStartDate(Date date) {
		GregorianCalendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		return calendar.getTime();
	}

	public static Timestamp getMonthStartTimestamp(Date date) {
		Date start = getMonthStartDate(date);
		return new Timestamp(start.getTime());
	}

	/**
	 * 获取下月开始时间
	 * 
	 * @param date
	 * @return
	 */
	public static Date getNextMonthStartDate(Date date) {
		GregorianCalendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);

		calendar.add(Calendar.MONTH, 1);

		return calendar.getTime();
	}

	public static Timestamp getNextMonthStartTimestamp(Date date) {
		Date start = getNextMonthStartDate(date);
		return new Timestamp(start.getTime());
	}

	/**
	 * 获取当年开始时间
	 * 
	 * @return
	 */
	public static Date getYearStartDate(Date date) {
		GregorianCalendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		calendar.set(Calendar.MONTH, 0);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		return calendar.getTime();
	}

	public static Timestamp getYearStartTimestamp(Date date) {
		Date start = getYearStartDate(date);
		return new Timestamp(start.getTime());
	}

	/**
	 * 获取下一年开始时间
	 * 
	 * @return
	 */
	public static Date getNextYearStartDate(Date date) {
		GregorianCalendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		calendar.set(Calendar.MONTH, 0);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);

		calendar.add(Calendar.YEAR, 1);

		return calendar.getTime();
	}

	public static Timestamp getNextYearStartTimestamp(Date date) {
		Date start = getNextYearStartDate(date);
		return new Timestamp(start.getTime());
	}

	/**
	 * 获取N天之后的具体时间;
	 * @param dateNum
	 * @return
	 */
	public static Date getAfterDate(int dateNum) {	
		Calendar calendar = Calendar.getInstance();
		int day = calendar.get(Calendar.DAY_OF_YEAR);
		calendar.set(Calendar.DAY_OF_YEAR, day + dateNum);
		Date beforeDate = calendar.getTime();
		return beforeDate;
	}
	
	/**
	 * 获取N天之后的具体时间;
	 * @param dateNum
	 * @return
	 */
	public static Date getAfterDateByDate(Date date,int dateNum) {	
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int day = calendar.get(Calendar.DAY_OF_YEAR);
		calendar.set(Calendar.DAY_OF_YEAR, day + dateNum);
		Date beforeDate = calendar.getTime();
		return beforeDate;
	}
	
	/**
	 * 获取N天之前的具体时间;
	 * @param dateNum
	 * @return
	 */
	public static Date getBeforeDate(int dateNum) {	
		Calendar calendar = Calendar.getInstance();
		int day = calendar.get(Calendar.DAY_OF_YEAR);
		calendar.set(Calendar.DAY_OF_YEAR, day - dateNum);
		Date beforeDate = calendar.getTime();
		return beforeDate;
	}
	
	/**
	 * 比较两个时间是否大于要比较时间；
	 * @param compareDate
	 * @param currentDate
	 * @return
	 */
	public static boolean isLargeCurrentDate(Date compareDate,Date currentDate){  
		
	    if(compareDate.after(currentDate)){  
	        return true;
	    }          
	    return false;
	} 
	
	
	/**
	 * 获取当年的月份
	 * @param currentdate
	 * @return
	 */
	public static int getMonthOfThisYear(Date currentdate){
		Calendar cal = Calendar.getInstance();
		if(currentdate !=null){
			cal.setTime(currentdate);
		}else{
		    cal.setTime(new Date());
		}
		return cal.get(Calendar.MONTH) + 1;
	}
	
	/**
	 * 获取当月的天份
	 * @param currentdate
	 * @return
	 */
	public static int getDayOfThisYear(Date currentdate) {
		Calendar cal = Calendar.getInstance();
		if(currentdate !=null){
			cal.setTime(currentdate);
		}else{
		    cal.setTime(new Date());
		}
		return cal.get(Calendar.DATE);
	}
	
	/**
	 * 获取当年的时间
	 * @param currentdate
	 * @return
	 */
	public static int getYearOfThisYear(Date currentdate) {
		Calendar cal = Calendar.getInstance();
		if(currentdate !=null){
			cal.setTime(currentdate);
		}else{
		    cal.setTime(new Date());
		}
		return cal.get(Calendar.YEAR);
	}
	
	/***
	 * 两个日期比较，返回相差天数
	 * @param sDateValue1
	 * @param sDateValue2
	 * @return
	 * @throws ParseException
	 */
	public static long getCompareDateNum(String sDateValue1,String sDateValue2) throws ParseException{
		long DAY = 24L * 60L * 60L * 1000L;    
		SimpleDateFormat df = new SimpleDateFormat( DATE_FORMAT );    
		Date d1 = df.parse( sDateValue1);    
		Date d2 = df.parse( sDateValue2 );    
				
		return (( d2.getTime() - d1.getTime() ) / DAY );
	}
	
	/**    
	 * 得到本月的今天    
	 *     
	 * @return    
	 */ 
	public static int getMonthOfToday(){   
        GregorianCalendar vTodayCal = new GregorianCalendar();   
        return vTodayCal.get(GregorianCalendar.DAY_OF_MONTH);   
    }  
	
	/**    
	 * 得到本月的最后一天    
	 *     
	 * @return    
	 */     
	public static String getMonthLastDay() {
		SimpleDateFormat sdf = new SimpleDateFormat("dd");
	    Calendar calendar = Calendar.getInstance();      
	    calendar.set(Calendar.DAY_OF_MONTH, calendar      
	            .getActualMaximum(Calendar.DAY_OF_MONTH));      
	    return sdf.format(calendar.getTime());    
	}
	
	
	/***
	 * 返回时间增加秒后的时间。
	 * @param date
	 * @param seecond
	 * @return
	 */
	public static Date getDateAddSecond(Date date,int seecond){
		Date  d = null;
		  if(date == null){
			  d = new Date();
		  }else{
			 d = date;
		  }
		  long l = d.getTime();
		  d.setTime(l);
		  d = new Date(l + seecond*1000 );
		return  d;
	}
	

}
